
package com.yimixian.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.ViewGroup;

import com.yimixian.app.R;
import com.yimixian.app.cart.CartManager;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.viewholder.CartItemViewHolder;
import com.ymx.sdk.widget.recyclerview.viewholder.AbsViewHolder;
import com.ymx.sdk.widget.recyclerview.ListRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by songzy on 2015/11/27.
 */
public class CartRecylerAdapter extends ListRecyclerAdapter<GoodsItem> {
    public CartRecylerAdapter(ArrayList<GoodsItem> datas, Context context) {
        super(datas, context);
        mContext = context;
        mList = datas;
    }

    @Override
    public int getItemLayoutResId() {
        return R.layout.cart_freebie_goods_item_view;
    }

    @Override
    public AbsViewHolder getItemViewHoler(ViewGroup parent, int viewType) {
        AbsViewHolder holder = new CartItemViewHolder(getItemView(parent, viewType), mContext);
        return holder;
    }

    @Override
    public void onBindViewHolder(AbsViewHolder holder, int position) {
        boolean isNormalView = true;
        int positionSize = 0;
        int goodsGroupPosition = 0;
        if (CartManager.getInstance().mGoodsGroup != null) {
            for (int i = 0; i < CartManager.getInstance().mGoodsGroup.size(); i++) {
                if (CartManager.getInstance().mGoodsGroup.get(i) != null
                        && CartManager.getInstance().mGoodsGroup.get(i).goods != null) {
                    if (CartManager.getInstance().mGoodsGroup.get(i).goods.size() > 0) {
                        positionSize += CartManager.getInstance().mGoodsGroup.get(i).goods.size();
                        if (position == 0) {
                            isNormalView = false;
                            goodsGroupPosition = i;
                            break;
                        }
                        if (position == positionSize) {
                            isNormalView = false;
                            goodsGroupPosition = i + 1;
                            break;
                        }
                    } else {
                        continue;
                    }
                }
            }
        }
        String title = "";
        if (CartManager.getInstance().mGoodsGroup != null
                && goodsGroupPosition < CartManager.getInstance().mGoodsGroup.size()) {
            if (CartManager.getInstance().mGoodsGroup.get(goodsGroupPosition) != null) {
                if (isNormalView) {
                    title = "";
                } else {
                    title = CartManager.getInstance().mGoodsGroup.get(goodsGroupPosition).title;
                }
            }
        }
        if (holder != null && holder instanceof CartItemViewHolder) {
            CartItemViewHolder viewHolder = (CartItemViewHolder) holder;
            viewHolder.setDatas(mList);
            viewHolder.setTitle(title);
            if (isNormalView) {
                viewHolder.hideTitleView();
            } else {
                viewHolder.showTitleView();
            }
            if (TextUtils.isEmpty(title)) {
                viewHolder.hideTitleView();
            }
        }
        super.onBindViewHolder(holder, position);
    }
}
